package com.superman.supermarket.client.view.cart;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Widget;
import com.superman.mvpframe.client.mvp.IPageViewPresenter;
import com.superman.mvpframe.client.widget.pulltorefresh.IPullToRefreshView;
import com.superman.mvpframe.client.widget.pulltorefresh.PullToRefreshViewAbstractImpl;
import com.superman.supermarket.client.composite.cart.CartStoreModule;
import com.superman.supermarket.client.composite.cart.bottom.CartBottomModule;
import com.superman.supermarket.client.composite.cart.invalid.InvalidSkuListModule;
import com.superman.supermarket.client.utils.ActivityJumpUrl;
import com.superman.supermarket.client.utils.SpecialConfigUtils;
import com.superman.supermarket.client.widget.header.PageHeader;

/**
 * 购物车页面实现类
 * 
 * @author superman
 * @version 2018年8月7日下午11:13:36
 */
public class CartViewImpl extends PullToRefreshViewAbstractImpl implements CartView {

	interface CartViewImplUiBinder extends UiBinder<Widget, CartViewImpl> {
	}

	private static CartViewImplUiBinder uiBinder = GWT.create(CartViewImplUiBinder.class);

	@UiField
	CartBottomModule cartbottommodule;
	@UiField
	InvalidSkuListModule invalidskulistmodule;
	@UiField
	PageHeader pageheader;
	@UiField
	HTML tipshtml;
	@UiField
	FlowPanel wrapperpanel, storelistpanel;

	/**
	 * 构造方法
	 * 
	 * @author superman
	 * @version 2018年8月7日 下午11:13:46
	 */
	public CartViewImpl() {
		initWidget(uiBinder.createAndBindUi(this));
	}

	@Override
	public void addInvalidSkusList(List<Map<String, Object>> invalidSkusList) {
		invalidskulistmodule.setVisible(false);
		if (invalidSkusList.size() > 0) {
			invalidskulistmodule.setListData(invalidSkusList);
			invalidskulistmodule.setVisible(true);
		}
	}

	@Override
	public List<Map<String, Object>> getCheckedStoreListData() {
		List<Map<String, Object>> storeList = new ArrayList<Map<String, Object>>();

		for (int i = 0; i < storelistpanel.getWidgetCount(); i++) {
			CartStoreModule cartStoreModule = (CartStoreModule) storelistpanel.getWidget(i);
			if (cartStoreModule.getData() != null)
				storeList.add(cartStoreModule.getData());
		}

		return storeList;
	}

	@Override
	public IPullToRefreshView getPullToRefreshView() {
		return this;
	}

	@Override
	public void init() {
		// TODO Auto-generated method stub

	}

	@UiHandler("tipshtml")
	void pushToSpecialPage(ClickEvent event) {
		ActivityJumpUrl.pushToSpecialSkuListPage(SpecialConfigUtils.getCartRecommandSpecialId(), "猜你喜欢");
	}

	@Override
	public void resetBottom() {
		cartbottommodule.reset();
	}

	@Override
	public void setBottomVisible(boolean visible) {
		cartbottommodule.setVisible(visible);
		tipshtml.setVisible(!visible);
	}

	@Override
	public void setDataListDivId(String id) {
		storelistpanel.getElement().setId(id);
	}

	@Override
	public void setListData(List<Map<String, Object>> list) {
		for (Map<String, Object> map : list) {
			CartStoreModule cartStoreModule = new CartStoreModule();
			cartStoreModule.setData(map);
			storelistpanel.add(cartStoreModule);
		}

		// 置入原始数据
		cartbottommodule.setListData(list);
	}

	@Override
	public void setMeScrollDivId(String id) {
		wrapperpanel.getElement().setId(id);
	}

	@Override
	public void setPageHeaderCartSkuNum(int skuNUm) {
		pageheader.setText("购物车(" + skuNUm + ")");
	}

	@Override
	public void setPageViewPresenter(IPageViewPresenter presenter) {
		// TODO Auto-generated method stub

	}

	@Override
	public void clearDataList() {
		storelistpanel.clear();
	}

}
